package mario;

import java.awt.image.BufferedImage;

public class Mario implements Runnable {
    //表示横纵坐标
    private int x;
    private int y;
    //表示马里奥状态
    private String status;
    //显示当前状态的图像
    private BufferedImage show = null;
    //获取障碍物信息
    private BackGround backGround = new BackGround();
    //实现马里奥动作
    private Thread thread = null;
    //马里奥移动
    private int move;
    //马里奥跳跃
    private int jump;
    //索引
    private int index;
    //马里奥上升时间
    private int upTime = 0;
    //马里奥是否死亡
    private boolean isDeath = false;
    //踩砖块
    private int brick = 0;
    //踩怪
    private int monster = 0;
    //重置马里奥
    public void reBirth(){
        isDeath = false;
    }
    //马里奥死亡方法
    public void death(){
        isDeath = true;
    }
    //马里奥向左移动
    public void moveLeft(){
        move = -5;
        //判断是否处于空中
        if (status.indexOf("jump")!=-1){
            status = "jump_left";
        }else {
            status = "move_left";
        }
    }
    //马里奥向右移动
    public void moveRight(){
        move = 5;
        //判断是否处于空中
        if (status.indexOf("jump")!=-1){
            status = "jump_right";
        }else {
            status = "move_right";
        }
    }
    //马里奥向左停止
    public void stopLeft(){
        move = 0;
        if (status.indexOf("jump")!=-1){
            status = "jump_left";
        }else {
            status = "stop_left";
        }
    }
    //马里奥向右停止
    public void stopRight(){
        move = 0;
        if (status.indexOf("jump")!=-1){
            status = "jump_right";
        }else {
            status = "stop_right";
        }
    }
    //马里奥跳跃
    public void jump(){
        if (status.indexOf("jump")==-1){
            if (status.indexOf("left")!=-1){
                status = "jump_left";
            }else {
                status = "jump_right";
            }
            jump = -10;
            upTime = 7;
        }
    }
    //马里奥下落
    public void fall(){
        if (status.indexOf("left")!=-1){
            status = "jump_left";
        }else {
            status = "jump_right";
        }
        jump = 10;
    }

    public Mario(int x, int y) {
        this.x = x;
        this.y = y;
        show = StaticValue.stand_R;
        status = "stop_right";
        thread = new Thread(this);
        thread.start();
    }

    public Mario() {
    }

    @Override
    public void run() {
        while (true){
            //是否在障碍物上
            boolean onObstacle = false;
            //是否可以向右走
            boolean canRight = true;
            //是否可以向左走
            boolean canLeft = true;
            //判断马里奥是否到达旗杆位置

            //遍历障碍物
            for (int i = 0;i < backGround.getObstacles().size();i++){
                Obstacle obstacle = backGround.getObstacles().get(i);
                //判断是否处于障碍物
                if (obstacle.getY() == this.y+30  && (obstacle.getX()>this.x-30 && obstacle.getX()<this.x+30)){
                    onObstacle = true;
                }
                //跳起来顶砖块
                if ((obstacle.getY()>=this.y-30 && obstacle.getY() <= this.y -20) && (obstacle.getX() > this.x-30 && obstacle.getX() < this.x+25)){
                    if (obstacle.getType() == 0){
                        backGround.getObstacles().remove(obstacle);
                        brick += 1;
                    }
                    upTime = 0;
                }
                //判断是否可以向右走
                if (obstacle.getX()==this.x+25 && (obstacle.getY() > this.y-30 && obstacle.getY() < this.y+25)){
                    canRight = false;
                }
                if (this.x==870) canRight = false;
                //判断是否可以向左走
                if (obstacle.getX()==this.x-25 && (obstacle.getY() > this.y-30 && obstacle.getY() < this.y+25)){
                    canLeft = false;
                }
            }
            //判断马里奥是否碰到敌人活着踩死敌人
            for (int i = 0;i < backGround.getEnemyList().size();i++){
                Enemy enemy = backGround.getEnemyList().get(i);
                if (enemy.getY() == this.y + 20 && (enemy.getX() -25 <= this.x && enemy.getX() + 30 >= this.x)){
                    if (enemy.getType() == 1){
                        enemy.death();
                        monster += 1;
                        upTime = 5;
                        jump = -10;
                    }else if (enemy.getType()==2){
                        //马里奥死亡方法
                        death();
                    }
                }
                if ((enemy.getX()+35 > this.x && enemy.getX() -25 < this.x) && (enemy.getY() + 35 >this.y && enemy.getY() - 20 < this.y)){
                    //马里奥死亡方法
                    death();
                }
                if (this.getY()>=600){
                    death();
                }
            }
            //跳跃
            if (onObstacle && upTime == 0){
                if (status.indexOf("left")!=-1){
                    if (move!=0){
                        status = "move_left";
                    }else {
                        status = "stop_left";
                    }
                }else {
                    if (move!=0){
                        status = "move_right";
                    }else {
                        status = "stop_right";
                    }
                }
            }else {
                if (upTime!=0){
                    upTime--;
                }else {
                    fall();
                }
                y += jump;
            }

            if ((canLeft && move < 0) || (canRight && move > 0)){
                x += move;
                //判断边界
                if (x<0) x=0;
            }
            //判断是否移动状态
            if (status.contains("move")){
                index = index == 0 ? 1 :0;
                //index = index == 1 ? 2 :0;
            }
            //判断是否向左移动
            if ("move_left".equals(status)){
                show = StaticValue.run_L.get(index);
            }
            //判断是否向右移动
            if ("move_right".equals(status)){
                show = StaticValue.run_R.get(index);
            }
            //判断是否向左停止
            if ("stop_left".equals(status)){
                show = StaticValue.stand_L;
            }
            //判断是否向右停止
            if ("stop_right".equals(status)){
                show = StaticValue.stand_R;
            }
            //判断是否向左跳跃
            if ("jump_left".equals(status)){
                show = StaticValue.jump_L;
            }
            //判断是否向右跳跃
            if ("jump_right".equals(status)){
                show = StaticValue.jump_R;
            }


            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public BufferedImage getShow() {
        return show;
    }

    public void setShow(BufferedImage show) {
        this.show = show;
    }

    public BackGround getBackGround() {
        return backGround;
    }

    public void setBackGround(BackGround backGround) {
        this.backGround = backGround;
    }

    public boolean isDeath() {
        return isDeath;
    }

    public int getBrick() {
        return brick;
    }

    public int getMonster() {
        return monster;
    }
}
